package com.wawayaya;

import android.content.Intent;
import com.creative.accountsdk.AccountSdk;
import org.kymjs.kjframe.KJBitmap;
import org.kymjs.kjframe.bitmap.BitmapConfig;
import org.kymjs.kjframe.utils.KJLoger;
import com.creative.accountsdk.BuildConfig;
import com.wawayaya.base.BaseApplication;
import com.wawayaya.cache.DataCleanManager;
import com.wawayaya.cache.UserDataCache;
import com.wawayaya.db.DBManager;
import com.wawayaya.util.Constants;
import com.wawayaya.util.FileUtil;
import com.wawayaya.util.MethodsCompat;
import com.wawayaya.util.TLog;
/**
 * 全局应用程序类：
 *   用于保存、调用全局应用配置
 * 
 * @author  David
 * @version 1.0
 * @created 2015-11-3
 */
public class AppContext extends BaseApplication {

    private static AppContext instance;
    public  static AccountSdk  accountSdk;
    public  static String      bookzipsavedpath;

    @Override
    public void onCreate() {
        super.onCreate();
        this.instance = this;
        this.init();
        FileUtil.copyArrestToDB(instance, "joyReader.db");
        this.shareSdk();
        this.initDB();
       // Thread.setDefaultUncaughtExceptionHandler(AppException.getAppExceptionHandler(this));
    }

    private void init() {
        // Log控制器
        KJLoger.openDebutLog(true);
       // TLog.DEBUG = BuildConfig.DEBUG;
        // Bitmap缓存地址
        BitmapConfig.CACHEPATH = "wawayaay/imagecache";
    }

    /**
     * 获得当前app运行的AppContext
     * 
     * @return
     */
    public static AppContext getInstance() {
        return instance;
    }


    /**
     * 用户注销
     */
    public void Logout() {
        UserDataCache.getInstance().clear(context());
        Intent intent = new Intent(Constants.INTENT_ACTION_LOGOUT);
        sendBroadcast(intent);
    }

    /**
     * 清除app缓存
     */
    public void clearAppCache() {
        DataCleanManager.cleanDatabases(this);
        // 清除数据缓存
        DataCleanManager.cleanInternalCache(this);
        // 2.2版本才有将应用缓存转移到sd卡的功能
        if (isMethodsCompat(android.os.Build.VERSION_CODES.FROYO)) {
            DataCleanManager.cleanCustomCache(MethodsCompat.getExternalCacheDir(this));
        }
        new KJBitmap().cleanCache();
    }

    /**
     * 判断当前版本是否兼容目标版本的方法
     * 
     * @param VersionCode
     * @return
     */
    public static boolean isMethodsCompat(int VersionCode) {
        int currentVersion = android.os.Build.VERSION.SDK_INT;
        return currentVersion >= VersionCode;
    }

    /**
     * share sdk config
     *
     */
    private void  shareSdk(){
        AccountSdk.clearInstance();
        accountSdk = AccountSdk.getInstance();
        accountSdk.init(this, Constants.SHARE_SDK_APP_ID, Constants.SHARE_SDK_SECRET_KEY);
    }

    /**
     * initDB
     */
    private void  initDB() {
        DBManager.init(this);
    }
}
